﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FESSC_MESSAGE;
using ECMessageQueue;
using System.Windows;

namespace FESSC_WORKBENCH_SERVER
{
    public class OP_USER
    {
        public static Func<MessageQueue.MQMESSAGE, bool> userLogin = delegate (MessageQueue.MQMESSAGE mqm)
         {
             bool ret = false;
             MsgUserLogin m = new MsgUserLogin(mqm.content);
             DB db = DB.getInstance();
             using (var context = db.getEntities())
             {
                 string uid = m.getUid();
                 string pwd = m.getPwdMD5();
                 var qry = context.Users.FirstOrDefault(u => u.uID == uid && u.uPSW == pwd);
                 if (qry != null)
                 {
                     qry.uLastLogin = DateTime.Now;
                     qry.uLoginCount++;
                     context.SaveChanges();
                     ret = true;
                 }
             }
             if (ret)
             {
                 AppVar.usertoken.addUser(mqm.token, m.getUid());
                 string debuglog = string.Empty;
                 foreach (System.Collections.DictionaryEntry de in AppVar.usertoken.getUserTokens())
                 {
                     debuglog += string.Format("{0}\t{1}\r\n", de.Key, de.Value);
                 }
                 AppVar.log.Debug("User Tokens:\r\nToken\t\t\tUid\r\n" + debuglog);
             }
             Msg rs = new Msg(false);
             rs.setHeader("USERLOGIN", 0, mqm.remote, mqm.token);
             rs.setRsCode(ret ? "OK" : "USERLOGIN-INVALIED");
             string rsxml = rs.ToString();
             MessageQueue.MQMESSAGE rsmqm = new MessageQueue.MQMESSAGE
             {
                 content = rsxml,
                 length = rsxml.Length,
                 remote = mqm.remote,
                 token = mqm.token
             };
             AppVar.mqserver.sendMessage(rsmqm);
             return ret;
         };

        public static Func<MessageQueue.MQMESSAGE, bool> userLogOut = delegate (MessageQueue.MQMESSAGE mqm)
        {
            var ret = false;
            MsgUserLogout m = new MsgUserLogout(mqm.content);
            var uid = AppVar.usertoken.getUid(mqm.token);
            var rquid = m.getUid();
            AppVar.usertoken.removeUserByToken(mqm.token);
            Msg rs = new Msg(false);
            rs.setHeader("USERLOGOUT", 0, mqm.remote, mqm.token);
            ret = uid == rquid ? true : false;
            rs.setRsCode(ret ? "OK" : "USERLOGOUT-INVALIED");
            var rsxml = rs.ToString();
            MessageQueue.MQMESSAGE rsmqm = new MessageQueue.MQMESSAGE()
            {
                content = rsxml,
                length = rsxml.Length,
                remote = mqm.remote,
                token = mqm.token
            };
            AppVar.mqserver.sendMessage(rsmqm);
            string debuglog = string.Empty;
            foreach (System.Collections.DictionaryEntry de in AppVar.usertoken.getUserTokens())
            {
                debuglog += string.Format("{0}\t{1}\r\n", de.Key, de.Value);
            }
            AppVar.log.Debug("User Tokens:\r\nToken\t\t\tUid\r\n" + debuglog);
            return ret;
        };

        public static Func<MessageQueue.MQMESSAGE, bool> userInfo = delegate (MessageQueue.MQMESSAGE mqm)
        {
            var ret = false;
            Msg m = new Msg(false);
            var uid = AppVar.usertoken.getUid(mqm.token);
            int deptid = 0, groupid = 0;
            string name = string.Empty, deptname = string.Empty, groupname = string.Empty;
            using (var db = AppVar.db.getEntities())
            {
                var qry = db.Users.FirstOrDefault(u => u.uID == uid);
                qry.DeptsReference.Load();
                qry.UserGroupReference.Load();
                if (qry != null)
                {
                    ret = true;
                    name = qry.uName;
                    deptid = qry.Depts.id;
                    deptname = qry.Depts.name;
                    groupid = qry.UserGroup.GroupID;
                    groupname = qry.UserGroup.gName;
                }
            }
            m.setHeader("USERINFO", 0, mqm.remote, mqm.token);
            if (ret)
            {
                m.setRsCode();
                var info = m.add("userinfo");
                m.add(info, "id", uid);
                m.add(info, "name", name);
                m.add(info, "deptid", deptid.ToString());
                m.add(info, "deptname", deptname);
                m.add(info, "groupid", groupid.ToString());
                m.add(info, "groupname", groupname);
            }
            else m.setRsCode("USERINFO-INVALIED");
            var mstr = m.ToString();
            MessageQueue.MQMESSAGE rsmqm = new MessageQueue.MQMESSAGE
            {
                content = mstr,
                length = mstr.Length,
                remote = mqm.remote,
                token = mqm.token
            };
            AppVar.mqserver.sendMessage(rsmqm);
            return ret;
        };

        public static Func<MessageQueue.MQMESSAGE, bool> deptInfo = delegate (MessageQueue.MQMESSAGE mqm)
        {

            return true;
        };
    }
}
